//获取应用实例
const app = getApp()
let stauts = 0;
// pages/welfareMall/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    winH: app.globalData.winH,
    tabBarT: app.globalData.tabH,
    scrollHeight: app.globalData.scrollHeight,
    title: "福利商城", //页面标题
    nobgHeader: "nobgHeader",
    autoplay: true,
    sortVisible: false,
    selObj: [],
    purchaseCount: 1,
    sendObj: [{
        type: "自提",
        active: ""
      },
      {
        type: "配送",
        active: ""
      }
    ],
    interval: 3000,
    indicatorDots: true,
    imgUrls: [],
    goodName: '',
    bgWhite: "bgWhite",
    scrollTop: 0,
    pdBottom: 20,
    currentPage: 1,
    pageSize: 8,
    totalPage: 1,
    sortIdArr: [],
    noData: false,
    time: null,
    goodList: [],
    usableIntegral: 0
  },
  scroll(e) {
    var scrollTop = e.detail.scrollTop;
    this.setData({
      scrollTop: scrollTop
    })
  },
  isTop() {
    // console.log("到顶了")
    this.setData({
      scrollY: true
    })
  },
  // 分类选择
  seled(e) {
    // var length = this.data.selObj.length;
    var index = e.target.dataset.index,
      activeItem = "selObj[" + index + "].active",
      nowItem = this.data.selObj[index].active,
      that = this,
      tempArr = [];

    switch (nowItem) {
      case 'active':
        that.setData({
          [activeItem]: ""
        })
        that.data.selObj.forEach(item => {
          if (item.active) {
            tempArr.push(item.goodstypeId)
          }
        })
        that.setData({
          sortIdArr: tempArr
        })
        break;
      case '':
        that.setData({
          [activeItem]: "active"
        })
        that.data.selObj.forEach(item => {
          if (item.active) {
            tempArr.push(item.goodstypeId)
          }
        })
        that.setData({
          sortIdArr: tempArr
        })
        break;
      default:
        break;
    }
  },
  // 分类查询
  sort() {
    // console.log(this.data.sortIdArr)
    this.setData({
      goodList: []
    })
    this.hideSort()
    this.getGoodList(this.data.sortIdArr, 1)
  },
  // 显示分类弹窗
  showSort() {
    this.setData({
      sortVisible: true
    })
  },
  // 隐藏分类弹窗
  hideSort() {
    this.setData({
      sortVisible: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.getBanner();
  },
  setgoodName(e) {
    var val = e.detail.value,
      that = this;
    that.setData({
      goodName: val
    })
    // clearTimeout(that.timer)
    // that.timer = setTimeout(function() {
    //   that.getGoodList();
    // }, 1000)
  },
  search(e) {
    var val = e.detail.value
    this.setData({
      goodName: val
    })
    this.getGoodList();
  },
  getBanner() {
    // console.log('banner')
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getBannerImg.do',
      method: 'post',
      success(res) {
        var imgArr = []
        res.data.forEach(item => {
          imgArr.push({
            // imgUrl: app.globalData.imgSever + item.imgUrl,
            imgUrl: item.imgUrl,
            mallproductId: item.mallproductId
          })
        })
        console.log('imgArr',res)
        that.setData({
          imgUrls: imgArr
        })
        that.selectComponent("#swiper").dotObjInit()
        that.selectComponent("#swiper").swiperInit()
        that.getGoodList(null, null, true);
      }
    })
  },
  // 获取购物车中商品数
  getCartMes() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'shopcard/getShopCartNum.do',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      success(res) {
        console.log('购物车', res)
        that.setData({
          shopCartNum: res.data.shopCartNum
        })
      }
    })
  },
  handleTapSearch() {
    this.getGoodList()
  },
  getGoodList(id, type, getSort) {
    const that = this,
      addtion = {}
    if (id) {
      addtion.goodsType = id
    }
    const data = {
      "userCode": wx.getStorageSync('userCode'),
      "goodsName": this.data.goodName,
      "currentPage": this.data.currentPage,
      "pageSize": this.data.pageSize,
      "queryType": type ? type : '',
      ...addtion
    }
    // console.log(data)
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getMallProduct.do',
      data: data,
      method: "post",
      success(res) {
        console.log('商品列表',  res.data)
        var list = that.pushData(res.data.productList)

        if (list) {
          if (list.length === 0) {
            that.setData({
              noData: true
            })
          } else {
            that.setData({
              noData: false
            })
          }
          that.setData({
            goodList: list,
            totalPage: res.data.pageInfo.totalPage
          })
          wx.hideLoading()
          if (getSort) {
            that.getSortList()
          }
        } else {
          wx.hideLoading()
        }
      }
    })
  },
  pushData(array) {
    const goodList = this.data.goodList
    array.forEach(item => {
      goodList.push(item)
    })
    return goodList
  },
  smartSort() {
    this.setData({
      goodList: []
    })
    this.getGoodList(null, 2)
  },
  canBuy() {
    this.setData({
      goodList: []
    })
    this.getGoodList(null, 3)
  },
  getSortList() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getTypes.do',
      method: 'post',
      success(res) {
        // console.log("分类", res)
        if (res.data.length > 0) {
          var data = res.data
          data.forEach(item => {
            item.active = ''
          })
          that.setData({
            selObj: data
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    // console.log(app.globalData)
    this.setData({
      tabBarT: app.globalData.tabH,
      winH: app.globalData.winH,
      tabH: app.globalData.tabH
    })
  },
  // 跳转页面函数
  toPage(e) {
    var url = e.currentTarget.dataset.url
    console.log(e.currentTarget.dataset.url)
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.pageInit()
  },
  // 初始化页面方法
  pageInit() {
    console.log('页面初始化')
    const that = this
    that.setData({
      currentPage: 1,
      totalPage: 1,
      goodList: []
    })
    that.getBanner()
    that.getCartMes()
    that.getHonor()
  },
  getHonor() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getYD.do',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      success(res) {
        console.log('雨点', res)
        that.setData({
          usableIntegral: res.data.usableIntegral
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    console.log('刷新')
    this.pageInit()
    setTimeout(function () {
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const currentPage = this.data.currentPage
    const totalPage = this.data.totalPage
    console.log(totalPage)
    if (currentPage < totalPage) {

      this.setData({
        currentPage: currentPage + 1
      })
      this.getGoodList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onPageScroll(event) {
    var nobgHeader = this.data.nobgHeader
    if (event.scrollTop > 10 && nobgHeader === 'nobgHeader') {
      this.setData({
        nobgHeader: ''
      })
    } else if (event.scrollTop < 10 && nobgHeader === '') {
      this.setData({
        nobgHeader: 'nobgHeader'
      })
    }
    this.setData({
      scrollTop: event.scrollTop
    })
  },
})